<?php

class controller extends yumi {

    public $sysclass;
    public $sitenav;

    public function __construct() {
        parent::__construct();

        //系统默认栏目
        global $sysclass;
        if (!empty($sysclass) && is_array($sysclass)) {
            $this->sysclass = $sysclass;
        } else {
            $this->sysclass = array(
                'zhengquan' => '证券',
                'licai' => '理财',
                'touzi' => '投资',
                'xiaofei' => '消费',
                'gongsi' => '公司',
                'xinwen' => '新闻',
                'btbkx' => '比特币快讯',
                'xgzb' => '选股指标',
                'btbzx' => '比特币资讯',
                'qkljs' => '区块链技术',
                'gpzs' => '股票知识',
                'gphq' => '股票行情',
                'gpzx' => '股票资讯',
                'gppz' => '股票配资',
                'qhzx' => '期货资讯',
                'qhwh' => '期货外汇',
                'hjzx' => '黄金资讯',
                'jjjg' => '基金价格'
            );
        }


        $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';

        $http_name = $_SERVER['HTTP_HOST'];

        /* if ($http_name!=$this->yuming_name && !preg_match("#^m\.#", $http_name) && !preg_match("#^www\.#", $http_name) && !preg_match("#^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$#", $http_name)) {
          header('HTTP/1.1 301 Moved Permanently');
          $request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
          $this->redirect($http_type."www.".$http_name.$request_uri);
          } */

        $GLOBALS['cfg_webname'] = $this->yuming_website['sitename'];
        $GLOBALS['cfg_keywords'] = $this->yuming_website['keywords'];
        $GLOBALS['cfg_description'] = $this->yuming_website['description'];
        $GLOBALS['cfg_basehost'] = $http_type . $this->yuming_name;
        $GLOBALS['cfg_clihost'] = $http_type . $this->yuming_name;
        $GLOBALS['cfg_powerby'] = "Copyright &copy; 2017 " . $this->yuming_website['sitename'] . " 版权所有";
        $GLOBALS['mobile_url'] = $GLOBALS['cfg_mobileurl'] = $this->mobile_url;

        $GLOBALS['cfg_mb_open'] = "";
        $GLOBALS['cfg_cmspath'] = "";
        $cfg_templets_dir = $GLOBALS['cfg_cmspath'] . '/templates';
        $GLOBALS['cfg_templeturl'] = $GLOBALS['cfg_clihost'] . $cfg_templets_dir;
        $GLOBALS['templets'] = $GLOBALS['cfg_templets_skin'] = $cfg_templets_dir . '/' . $this->yuming_website['template'];

        $global_isjump = empty($GLOBALS['G']['isjump'])?0:1;
        $global_jumurl = $GLOBALS['G']['jumpurl'];
        $nav = get_classlist();
        $this->sitenav = $nav;
        //dump($this);
    }

    public static function redirect($url, $msg = null) {
        if (!empty($url)) {
            if (defined('APPNAME')) {
                $url = str_replace('__APPROOT__', '/' . APPNAME, $url);
            }
            if (IS_AJAX) {
                if ($msg) {
                    echo ("<script>cpAlert('$msg', 0);</script>");
                }
                echo ("<script>$.pjax({url: '$url', container: '.rightframe', fragment:'body', timeout:8000 });</script>");
            } else {
                if ($msg) {
                    echo ("<script>alert('$msg', 0);</script>");
                }
                echo ("<script>window.location.href='$url';</script>");
            }
            exit;
        }
    }

    public static function redirect2($url) {
        if (!empty($url)) {
            if (defined('APPNAME')) {
                $url = str_replace('__APPROOT__', '/' . APPNAME, $url);
                header("location:" . $url);
            }
        }
    }

    public static function json($data) {
        global $debug;
        if ($debug || !empty($_GET['formatjson'])) {
            echo json_encode($data);
        } else {
            header('Content-type: application/json');
            echo json_encode($data);
        }
        exit;
    }

    public static function jsonp($data, $msg = null) {
        header('Content-type: application/javascript');
        $callback = gp("callback", 'dash');
        if (empty($callback))
            $callback = gp("jsoncallback", 'dash');
        echo "$callback(" . json_encode($data) . ")";
        exit;
    }

    public static function page_404() {
        header('HTTP/1.1 404 Not Found');
        header("Status: 404 Not Found");
        if (self::tpl('404', true)) {
            require self::tpl('404');
        } else {
            echo 'HTTP/1.1 404 Not Found';
        }

        exit;
    }

    public static function page_503() {
        header('HTTP/1.1 503 Service Temporarily Unavailable');
        header('Status: 503 Service Temporarily Unavailable');
        header('Retry-After: 60');
    }

    public static function tpl($tplName, $onlyCheck = false) {
        if (defined('APPNAME')) {
            $path = ROOT . DS . APPNAME . DS . 'templates';
            $cachepath = DATA . DS . 'tplcache' . DS . APPNAME;
            $tplName = $tplName . '.htm';
        } else {
            if (!empty($GLOBALS['W']['template'])) {

                $path = ROOT . DS . 'templates/' . $GLOBALS['W']['template'];
                $cachepath = DATA . DS . 'tplcache/' . $GLOBALS['W']['template'];
            } else {
                $path = ROOT . DS . 'templates/' . $GLOBALS['G']['template'];
                $cachepath = DATA . DS . 'tplcache/' . $GLOBALS['G']['template'];
            }
            if (defined('IS_MOBILE_SITE') && IS_MOBILE_SITE_OPEN) {
                $tplName = $tplName . '_m.htm';
            } else {
                $tplName = $tplName . '.htm';
            }

            if (!file_exists($path . '/' . $tplName)) {
                $path = ROOT . DS . 'templates/default';
            }
        }

        if ($onlyCheck) {
            return is_file($path . DS . $tplName);
        } else {

            $ins = new template($tplName, $path, $cachepath);
            return $ins->view();
        }
    }
    
    
    public static function jsctpl($tplName, $onlyCheck = false) {
        $appname = 'jsc1';
        $path = ROOT . DS . 'jsctpl' . DS .$appname;
        $cachepath = DATA . DS . 'tplcache' . DS . $appname;
        $tplName = $tplName . '.htm';
        if ($onlyCheck) {
            return is_file($path . DS . $tplName);
        } else {
            $ins = new template($tplName, $path, $cachepath);
            return $ins->view();
        }
    }

}
